fix(iOS): prevent search bar from reopening keyboard on header button press#3676
fix(iOS): prevent search bar from reopening keyboard on header button press#3676just1and0 wants to merge 1 commit intosoftware-mansion:mainfrom
Conversation
… press When a screen has both a search bar and header buttons, tapping a header button after dismissing the search bar keyboard causes the keyboard to reopen unexpectedly. This happens because: 1. updateViewController unconditionally reassigns navitem.searchController on every render cycle, causing UIKit to re-activate the search bar. 2. searchBarCancelButtonClicked does not resign first responder on the actual UISearchBar, only on the RNSSearchBar wrapper. This commit guards the searchController assignment to only occur when the value changes, and ensures the native search bar properly resigns first responder when cancelled.
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug on iOS where tapping a header button after dismissing the search bar keyboard causes the keyboard to reopen unexpectedly. The fix involves preventing UIKit from re-activating the search bar when navigation properties are updated, and ensuring the search bar properly releases first responder status when cancelled.
Changes:
- Guarded
navitem.searchControllerassignment to prevent UIKit from re-activating the search bar on every React re-render - Added explicit
resignFirstRespondercall on the native UISearchBar when the cancel button is clicked - Added test file
TestSearchBarKeyboardReopen.tsxto verify the fix
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| ios/RNSScreenStackHeaderConfig.mm | Guards searchController assignment to only update when value changes, preventing UIKit from re-activating search bar |
| ios/RNSSearchBar.mm | Adds resignFirstResponder on native UISearchBar in cancel handler to ensure keyboard is fully dismissed |
| apps/src/tests/issue-tests/TestSearchBarKeyboardReopen.tsx | Test case demonstrating the fix with search bar + header button interaction |
| apps/src/tests/issue-tests/index.ts | Exports new test file |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 1. Tap the search bar to focus it{'\n'} | ||
| 2. Dismiss the keyboard (tap Cancel or tap away){'\n'} | ||
| 3. Tap the "Menu" button in the header{'\n'} | ||
| 4. Bug: The keyboard opens again instead of just triggering the button |
There was a problem hiding this comment.
The description in the comment says "4. Bug: The keyboard opens again instead of just triggering the button" but this is describing the bug that was fixed, not the current expected behavior. Since this is a test file documenting the fix, the comment should clarify that this was the previous buggy behavior, or update it to describe the expected behavior after the fix (keyboard should NOT reopen).
| 4. Bug: The keyboard opens again instead of just triggering the button | |
| 4. Expected: The keyboard should remain dismissed and only the button press should be handled (previously, the keyboard would reopen) |
Description
Closes #3677.
When a screen has both a
headerSearchBarOptionssearch bar and header buttons (e.g.headerRight), tapping a header button after dismissing the search bar keyboard causes the keyboard to reopen unexpectedly.Repro steps:
Minimal reproduction: https://github.com/sbkl/sdk-55-header-right-button-opening-keyboard
Also reported on Expo: expo/expo#43198
Changes
Two fixes in the iOS native code:
RNSScreenStackHeaderConfig.mm: Guard thenavitem.searchControllerassignment to only occur when the value actually changes. Previously, this was unconditionally reassigned on everyupdateViewControllercall, which caused UIKit to re-activate the search bar and trigger the keyboard.RNSSearchBar.mm: InsearchBarCancelButtonClicked:, also callresignFirstResponderon the actual_controller.searchBar(the nativeUISearchBar), not just onself(theRNSSearchBarwrapper). This ensures the search bar fully gives up keyboard focus when cancelled.Before & after
Before (bug)
Screen.Recording.2026-02-17.at.9.09.11.AM.mov
After focusing and dismissing the search bar, tapping the header menu button reopens the keyboard.
After (fix)
After the fix, tapping the header menu button correctly triggers the menu action without reopening the keyboard.
Test plan
TestSearchBarKeyboardReopen.tsxinapps/src/tests/issue-tests/Checklist